import { ComponentInfoType, ComponentsStateType } from '@/store/features/componentsReducer'

export const insertNewComponent = (draft: ComponentsStateType, newComponent: ComponentInfoType) => {
  const { selectedId, componentList } = draft
  const index = componentList.findIndex(c => c.fe_id === selectedId)

  if (index < 0) {
    //未选中任何组件
    draft.componentList.push(newComponent)
  } else {
    //选中了组件，插入到index后面
    draft.componentList.splice(index + 1, 0, newComponent)
  }

  draft.selectedId = newComponent.fe_id
}
